﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Id;
using System.Data;
using System.Data.SqlClient;

namespace FantastPlatform.Framework.Core
{
    public class IdGenerator : IIdentifierGenerator
    {

        public object Generate(NHibernate.Engine.ISessionImplementor session, object obj)
        {
            return GetAutoId(obj.GetType().Name);
        }
        private int GetAutoId(string entityType)
        {
            int id = 0;
            try
            {
                string sql = System.Configuration.ConfigurationManager.ConnectionStrings["GpsPatrol"].ToString();
                SqlConnection conn = new SqlConnection(sql);
                SqlCommand command = new SqlCommand("S_GET_SEQ_CUR_VAL", conn);
                command.CommandType = System.Data.CommandType.StoredProcedure;

                command.Parameters.Add("@SEQ_NAME", SqlDbType.VarChar);
                command.Parameters["@SEQ_NAME"].Value = entityType;

                //添加输出参数
                command.Parameters.Add("@CURVAL", SqlDbType.Int);
                command.Parameters["@CURVAL"].Direction = ParameterDirection.Output;
                conn.Open();
                SqlDataReader reader = command.ExecuteReader();
                id = Convert.ToInt32(command.Parameters["@CURVAL"].Value);
            }
            catch (System.Exception ex)
            {
            }
            return id;
        }
    }
}
